//
//  WLGuideViewController.m
//  WanLian
//
//  Created by muzhuang on 2018/10/24.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLGuideViewController.h"

#define GuidePageNum 3

@interface WLGuideViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIButton *enterButton;

@end

@implementation WLGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIScrollView *pagingScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    pagingScrollView.delegate = self;
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.bounces = YES;
    pagingScrollView.showsHorizontalScrollIndicator = NO;
    pagingScrollView.contentSize = CGSizeMake(self.view.frame.size.width *GuidePageNum, self.view.frame.size.height);
    [self.view addSubview:pagingScrollView];
    
    CGFloat bottom = 50;
    NSString *strPic = @"page_6_";
    if (HEIGHT_SCREEN_WL>=812)
    {
        bottom = 110;
        strPic = @"page_x_";
    }
    for (int i = 0; i<GuidePageNum; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*i, 0, self.view.frame.size.width, self.view.frame.size.height)];
        NSString *mageName = [NSString stringWithFormat:@"%@%d",strPic,i+1];
        imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:mageName ofType:@"jpg"]];
        [pagingScrollView addSubview:imgView];
    }
    
    _enterButton = [[UIButton alloc] initWithFrame:CGRectMake((WIDTH_SCREEN_WL-140)/2.0, self.view.frame.size.height-bottom-60, 140, 60)];
    //    self.enterButton.layer.cornerRadius = 5;
    //    self.enterButton.layer.masksToBounds = YES;
    //    [self.enterButton setTitle:@"立即体验" forState:UIControlStateNormal];
    [self.enterButton addTarget:self action:@selector(enterAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.enterButton];
    self.enterButton.hidden = YES;
}

- (void)enterAction
{
    if (self.didSelectedEnter) {
        self.didSelectedEnter();
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat contentPageX = (GuidePageNum-1)*self.view.frame.size.width;
    NSInteger currentPage = roundf(scrollView.contentOffset.x/WIDTH_SCREEN_WL);
    CGFloat currentOffsetX = scrollView.contentOffset.x;
    if (currentPage>=GuidePageNum-1&&(currentOffsetX-80)>contentPageX) {
        if (self.didSelectedEnter) {
            self.didSelectedEnter();
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger currentPage = roundf(scrollView.contentOffset.x/WIDTH_SCREEN_WL);
    if (currentPage >= GuidePageNum - 1) {
        self.enterButton.hidden = NO;
    }
    else{
        self.enterButton.hidden = YES;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
